package com.hpli.domain.award.service;


import com.hpli.domain.award.event.SendAwardMessageEvent;
import com.hpli.domain.award.model.aggregate.UserAwardRecordAggregate;
import com.hpli.domain.award.model.entity.DistributeAwardEntity;
import com.hpli.domain.award.model.entity.TaskEntity;
import com.hpli.domain.award.model.entity.UserAwardRecordEntity;
import com.hpli.domain.award.model.valobj.TaskStateVO;
import com.hpli.domain.award.repository.IAwardRepository;
import com.hpli.domain.award.service.distribute.IDistributeAward;
import com.hpli.types.event.BaseEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @Author: 汀
 * @CreateTime: 2025-04-16
 * @Description: 奖品服务
 * @Version: 1.0
 */

@Slf4j
@Service
public class AwardService implements IAwardService{

    private final IAwardRepository awardRepository;

    private final SendAwardMessageEvent sendAwardMessageEvent;

    private final Map<String, IDistributeAward> distributeAwardMap; // 奖品分发>

    public AwardService(IAwardRepository awardRepository, SendAwardMessageEvent sendAwardMessageEvent, Map<String, IDistributeAward> distributeAwardMap) {
        this.awardRepository = awardRepository;
        this.sendAwardMessageEvent = sendAwardMessageEvent;
        this.distributeAwardMap = distributeAwardMap;
    }

    @Override
    public void saveUserAwardRecord(UserAwardRecordEntity userAwardRecordEntity) {
        // 构建消息对象
        SendAwardMessageEvent.SendAwardMessage sendAwardMessage = new SendAwardMessageEvent.SendAwardMessage();
        sendAwardMessage.setUserId(userAwardRecordEntity.getUserId());
        sendAwardMessage.setOrderId(userAwardRecordEntity.getOrderId());
        sendAwardMessage.setAwardId(userAwardRecordEntity.getAwardId());
        sendAwardMessage.setAwardTitle(userAwardRecordEntity.getAwardTitle());
        sendAwardMessage.setAwardConfig(userAwardRecordEntity.getAwardConfig());

        BaseEvent.EventMessage<SendAwardMessageEvent.SendAwardMessage> eventMessage =
                sendAwardMessageEvent.buildEventMessage(sendAwardMessage);

        // 构建任务对象
        TaskEntity taskEntity = new TaskEntity();
        taskEntity.setUserId(userAwardRecordEntity.getUserId());
        taskEntity.setTopic(sendAwardMessageEvent.topic());
        taskEntity.setMessageId(eventMessage.getId());
        taskEntity.setMessage(eventMessage);
        taskEntity.setState(TaskStateVO.create);

        // 构建聚合对象
        UserAwardRecordAggregate userAwardRecordAggregate = UserAwardRecordAggregate.builder()
                .userAwardRecordEntity(userAwardRecordEntity)
                .taskEntity(taskEntity)
                .build();

        // 保存用户中奖记录
        awardRepository.saveUserAwardRecord(userAwardRecordAggregate);

        log.info("保存用户中奖记录成功，userId：{}, orderId：{}", userAwardRecordEntity.getUserId(), userAwardRecordEntity.getOrderId());
    }

    @Override
    public void distributeAward(DistributeAwardEntity distributeAward) {
        String awardKey = awardRepository.queryAwardKey(distributeAward.getAwardId());
        if (null == awardKey) {
            log.error("分发奖品失败，奖品ID不存在，awardId：{}", distributeAward.getAwardId());
            return;
        }

        IDistributeAward distributeAward1 = distributeAwardMap.get(awardKey);

        if (null == distributeAward1) {
            log.error("分发奖品失败，对应的服务不存在，awardKey：{}", awardKey);
            throw new RuntimeException("分发奖品，奖品" + awardKey + "对应的服务不存在");
        }

        // 发放奖品
        distributeAward1.giveOutPrizes(distributeAward);
    }
}
